#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <unistd.h>

#include <sys/ioctl.h>

#define CPULOAD_IOC_MAGIC			'C'
#define CPULOAD_GETVALUE			_IOR(CPULOAD_IOC_MAGIC, 1, unsigned long long)
#define CPULOAD_SETTIME				_IOW(CPULOAD_IOC_MAGIC, 2, unsigned int)

int main(int argc, char **argv)
{
	unsigned long long value[2] = {0, 0};
	unsigned int delay_time = 0;

	int fd = open("/dev/cpuload", O_RDWR);
	
	if(0 <= fd)
	{
		if(2 == argc)
		{
			delay_time = atoi(argv[1]);
			
			if(0 == ioctl(fd, CPULOAD_SETTIME, &delay_time))
			{
				printf("delay_time: %u\n", delay_time);
			}
			else
			{
				printf("delay_time set failed: %u\n", delay_time);
			}
		}

		while(1)
		{
			if(0 == ioctl(fd, CPULOAD_GETVALUE, value))
			{
				printf("idle: %llu, total: %llu, per: %0.2f\n", value[0], value[1], (value[0] * 1.0) / (value[1] * 1.0) * 100);
			}
			else
			{
				printf("delay_time get failed\n");
			}
			
			sleep(1);
		}

		close(fd);
	}
	return 0;
}